using UnityEngine;
using Yoozoo.Libs;

namespace Gameplay.PVE
{
    public class KnockBackAction : SpecialActionBase
    {
        private float startTime;
        private float knockBackSpeed = 0.3f;
        private float knockBackVerticalSpeed = 0.3f;
        private float accelerate = 1;
        private float accelerateVertical = 1;
        private float actionDuration;
        private float height;
        private Vector3 forward;

        public override void Start(AddControlArg arg)
        {
            base.Start(arg);
            startTime = TimeManager.logicTime;
            arg.forward.y = 0;
            forward = arg.forward;
            actionDuration = arg.time;
            height = arg.height;
            accelerate = -arg.distance * 2 / actionDuration / actionDuration;
            accelerateVertical = -height * 4 / actionDuration / actionDuration;
            knockBackSpeed = -accelerate * actionDuration;
            knockBackVerticalSpeed = -accelerateVertical * actionDuration / 2;
        }

        public override void Free()
        {
            unit.Data.position = new Vector3(unit.Data.position.x,0,unit.Data.position.z);
            base.Free();
        }

        public override void Update()
        {
            if (state == ESpecialActionState.Playing && TimeManager.logicTime - startTime <= actionDuration)
            {
                //forward = new Vector3(0.5f, 0, 0);
                var lastPosition = unit.Data.position;
                unit.Data.position += knockBackSpeed * TimeManager.LogicDeltaTime * forward +
                                      knockBackVerticalSpeed * TimeManager.LogicDeltaTime * Vector3.up;
                bool needChange = false;
                var screenPosition = PveCameraManager.Instance.mainCamera.WorldToScreenPoint(unit.Data.position);
                if (screenPosition.x < 50)
                {
                    screenPosition.x = 51;
                    needChange = true;
                }

                if (screenPosition.x > Screen.width - 50)
                {
                    screenPosition.x = Screen.width - 51;
                    needChange = true;
                }

                if (needChange)
                {
                    var newPosition = PveCameraManager.Instance.mainCamera.ScreenToWorldPoint(screenPosition);
                    newPosition.y = 0;
                    unit.Data.position = newPosition;
                }

                /*if (!PveFindPathManager.Instance.CheckIsWalkable(unit.Data.position, unit.Data.id))
                {
                    unit.Data.position = lastPosition;
                }*/
                unit.Data.position = PveManager.Instance.GetRestrictedPositionByBattleRegion(unit.Data.position);
                /*if (!PveManager.Instance.IsInBattleRegion(unit.Data.position))
                {
                    unit.Data.position = lastPosition;
                }*/
                knockBackSpeed += accelerate * TimeManager.LogicDeltaTime;
                knockBackVerticalSpeed += accelerateVertical * TimeManager.LogicDeltaTime;
            }
            else
            {
                unit.Data.position = new Vector3(unit.Data.position.x,0,unit.Data.position.z);
                state = ESpecialActionState.None;
            }
        }
    }
}